package com.example.demo.utils.rich;

import com.example.demo.utils.regex.RegexUtil;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: cj
 * @create: 14-01-12 14:22:20
 */
public class TextUtil {
    private static Pattern p_image = Pattern.compile( "<img.*src\\s*=\\s*(.*?)[^>]*?>", Pattern.CASE_INSENSITIVE );
    private static Pattern r_image = Pattern.compile( "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)" );

    /**
     * 提取富文本中纯文本
     */
    public static String getText(String richText) {
        String regx1 = "(\\n|[ \\t])*<([^>\"']*|\"[^\"]*\"|'[^']*')*>(\\n|[ \\t])*";
        Matcher matcher = Pattern.compile( regx1 ).matcher( richText );
        while (matcher.find()) {
            // 替换图片
            richText = matcher.replaceAll( "" ).replace( " ", "" ).replace( "space", "" );
        }
        return richText;
    }

    /**
     * 提取富文本中图片地址
     */
    public static List<String> getImgStr(String richText) {
        List<String> list = new ArrayList<>();
        Matcher pMatcher = p_image.matcher( richText );
        while (pMatcher.find()) {
            // 得到<img />数据
            String img = pMatcher.group();
            // 匹配<img>中的src数据
            Matcher rMatcher = r_image.matcher( img );
            while (rMatcher.find()) {
                list.add( rMatcher.group( 1 ) );
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String str = "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\"><span style=\"font-family: 宋体;\">在日常的生活中，我们往往会见到一些复杂的疾病，其实肉瘤就是其中的一种，在一些时候，忽然感觉到光滑的皮肤上出现一个脓包，所以感觉非常的不自在，那么肉瘤是怎么形成的呢！</span></span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><img src=\"http://192.168.1.76:8750/source/png/202112081744265773.png\" alt=\"\" width=\"750\" height=\"500\" /></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\"><span style=\"font-family: 宋体;\">肉瘤是怎么形成的</span></span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\"><span style=\"font-family: 宋体;\">脂肪瘤的形成原因有很多种，具体原因是什么尚不十分明确，主要有以下几种：</span></span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\">space</span><img src=\"http://192.168.1.76:8750/source/jpg/202112061133546702.jpg\" width=\"500\" height=\"459\" /></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\">1<span style=\"font-family: 宋体;\">、由于此类肿瘤好发于富含脂肪的部位，因此肥胖患者就要比一般的患者发病的机率大一些。</span></span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\">space</span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\">2<span style=\"font-family: 宋体;\">、中医认为脂肪瘤的产生与肝脾有关，因此经常生气，烦燥不安，忧思过度的人比较容易患这类疾病。</span></span></p>\n" +
                "<p class=\"p\" style=\"text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; line-height: 18.0000pt; background: #ffffff; margin: 0.0000pt;\"><span style=\"font-family: Arial; letter-spacing: 0pt; font-size: 12pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;\">3<span style=\"font-family: 宋体;\">、日常饮食不规律也会伤及肝脾，尤其是长期不吃早餐的患者和长期吃一些高胆固醇类食物的人最容易患这类疾病。</span></span></p>";
        String regx1 = "(\\n|[ \\t])*<([^>\"']*|\"[^\"]*\"|'[^']*')*>(\\n|[ \\t])*";
        Matcher matcher = Pattern.compile( regx1 ).matcher( str );
        while (matcher.find()) {
            // 替换图片
            str = matcher.replaceAll( "" ).replace( " ", "" ).replace( "space", "" );
        }
        System.out.println( str );
    }

}
